package conversor;

public class MathConversor {

	private String tableOctal[][], tableHexa[][];

	public MathConversor() {
		tableOctal = new String[][] { { "000", "0" }, { "001", "1" },
				{ "010", "2" }, { "011", "3" }, { "100", "4" }, { "101", "5" },
				{ "110", "6" }, { "111", "7" } };

		tableHexa = new String[][] { { "0000", "0" }, { "0001", "1" },
				{ "0010", "2" }, { "0011", "3" }, { "0100", "4" },
				{ "0101", "5" }, { "0110", "6" }, { "0111", "7" },
				{ "1000", "8" }, { "1001", "9" }, { "1010", "A" },
				{ "1011", "B" }, { "1100", "C" }, { "1101", "D" },
				{ "1110", "E" }, { "1111", "F" } };

	}

	public String decimalBinario(int decimal) {

		if (decimal == 0)
			return "0";

		String binario = "";

		while (decimal != 0) {
			binario = Integer.toString(decimal % 2) + binario;
			decimal = decimal / 2;
		}

		return binario;
	}

	public int binarioDecimal(String binario) {
		int decimal = 0;

		int pow = 0;
		for (int i = (binario.length() - 1); i >= 0; i--) {
			decimal += Integer.parseInt(binario.substring(i, i + 1))
					* Math.pow(2, pow);
			pow++;
		}

		return decimal;
	}

	public String decimalOctal(int decimal) {

		String octal = "";
		if (decimal == 0)
			return "0";

		while (decimal != 0) {
			octal = Integer.toString(decimal % 8) + octal;
			decimal = decimal / 8;
		}

		return octal;
	}

	public String decimalHexadecimal(int decimal) {

		String hexad = "";
		String[] simbols = { "A", "B", "C", "D", "E", "F" };

		if (decimal == 0)
			return "0";

		while (decimal != 0) {
			int resto = decimal % 16;
			if (resto < 10) {
				hexad = Integer.toString(resto) + hexad;
			} else {
				hexad = simbols[resto - 10] + hexad;
			}
			decimal = decimal / 16;
		}

		return hexad;
	}

	public int octalDecimal(String octal) {
		int decimal = 0;

		int pow = 0;
		for (int i = (octal.length() - 1); i >= 0; i--) {
			decimal += Integer.parseInt(octal.substring(i, i + 1))
					* Math.pow(8, pow);
			pow++;
		}

		return decimal;
	}

	// A0
	public int hexadecimalDecimal(String hexad) {
		hexad = hexad.toUpperCase();
		int decimal = 0;
		int pow = 0;
		for (int i = (hexad.length() - 1); i >= 0; i--) {
			String simbol = hexad.substring(i, i + 1);
			if (simbol.matches("\\d")) {
				decimal += Integer.parseInt(simbol) * Math.pow(16, pow);
			} else {
				if (simbol.equals("A")) {
					decimal += 10 * Math.pow(16, pow);
				} else if (simbol.equals("B")) {
					decimal += 11 * Math.pow(16, pow);
				} else if (simbol.equals("C")) {
					decimal += 12 * Math.pow(16, pow);
				} else if (simbol.equals("D")) {
					decimal += 13 * Math.pow(16, pow);
				} else if (simbol.equals("E")) {
					decimal += 14 * Math.pow(16, pow);
				} else {
					decimal += 15 * Math.pow(16, pow);
				}
			}

			pow++;
		}

		return decimal;
	}

	// 1010
	public String binarioOctal(String binario) {

		String octal = "";

		for (int i = (binario.length()); i > 0; i -= 3) {
			if (i < 3) {
				octal = pesquisaOctal(binario.substring(0, i), 0) + octal;
			} else {
				octal = pesquisaOctal(binario.substring(i - 3, i), 0) + octal;
			}
		}

		
		return octal;
	}
	//31
	public String octalBinario(String octal){
		String binario = "";
		
		for(int i = 0; i < octal.length();i++){
			binario += pesquisaOctal(octal.substring(i,i+1), 1);
		}
		
		binario = binario.replaceAll("0*(.+)", "$1");

		return binario;
	}

	public String hexaBinario(String hexa){
		String binario = "";
		
		for (int i = 0; i < hexa.length(); i++) {
			binario += pesquisaHexa(hexa.substring(i,i+1), 1);
		}
		
		binario = binario.replaceAll("0*(.+)","$1");
		
		return binario;
	}
	
	public String binarioHexa(String binario){
		String hexa = "";
		
		for(int i = binario.length(); i >= 0; i-= 4){
			if(i < 4){
				hexa = pesquisaHexa(binario.substring(0,i), 0) + hexa;
			}else{
				hexa = pesquisaHexa(binario.substring(i-4,i), 0) + hexa;
			}
		}
		hexa = hexa.replaceAll("0*(.+)","$1");
		
		return hexa;
	}
	
	public String pesquisaOctal(String termo, int opcao) {
		if (opcao == 0) {
			for (int i = 0; i < tableOctal.length; i++) {
				if (tableOctal[i][0].matches("0*" + termo)) {
					return tableOctal[i][1];
				}
			}
		}else{
			for (int i = 0; i < tableOctal.length; i++) {
				if (tableOctal[i][1].equals(termo)) {
					return tableOctal[i][0];
				}
			}
		}
		return "";
	}

	public String pesquisaHexa(String termo,int opcao){
		termo = termo.toUpperCase();
		if(opcao == 0){
			for (int i = 0; i < tableHexa.length; i++) {
				if(tableHexa[i][0].matches("0*" + termo)){
					return tableHexa[i][1];
				}
			}
		}else{
			for (int i = 0; i < tableHexa.length; i++) {
				if(tableHexa[i][1].equals(termo)){
					return tableHexa[i][0];
				}
			}
			
		}
		
		return "";
	}
	
	
	public static void main(String[] args) {
		MathConversor math = new MathConversor();
		
		System.out.println(math.decimalBinario(10));
		System.out.println(math.decimalOctal(10));
		System.out.println(math.decimalHexadecimal(18));
		System.out.println(math.binarioDecimal("1010"));
		System.out.println(math.hexadecimalDecimal("A1"));
		System.out.println(math.octalDecimal("10"));
		System.out.println(math.binarioOctal("111010"));
		System.out.println(math.binarioHexa("111010"));
		System.out.println(math.octalBinario("31"));
		System.out.println(math.hexaBinario("F1"));
		
	}

}

